package com.ebt.m.proposal_v2.utils;

import android.view.View;

import com.ebt.m.proposal_v2.interfaces.OnRxClickListener;
import com.ebt.m.commons.utils.Logger;
import com.jakewharton.rxbinding2.view.RxView;

import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

/**
 * @author archer.qi
 *         Created on 2017/5/18.
 */
public class RxUtils {

    public static <T> Observable<T> compose(Observable<T> observable) {
        return observable.subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread());
    }

    /**
     * Rx点击事件
     *
     * @param view
     * @param listener
     */
    public static void click(View view, OnRxClickListener listener) {
        RxView.clicks(view)
                .throttleFirst(500, TimeUnit.MILLISECONDS)
                .subscribe(new Observer<Object>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(Object value) {
                        if (listener != null) {
                            listener.onRxClick(view);
                        }
                    }

                    @Override
                    public void onComplete() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        e.printStackTrace();
                        Logger.e("RxUtils.click error: " + e.getMessage());
                    }
                });
    }

}
